const IMAGE_PATH = "./images/";
const IMAGES = [
  ["background", `${IMAGE_PATH}bg.jpg`],
  ["enemy", `${IMAGE_PATH}enemy.png`]
]

let instance;

export class ResourceLoader {

   constructor() {
      this.imageMap = new Map(IMAGES);
      for (let [key, value] of this.imageMap) {
        let img = new Image();
        img.src = value;
        this.imageMap.set(key, img);
      }
      console.log(this.imageMap);
   }

   static getInstance() {
     if (!instance) {
       instance = new ResourceLoader();
     }
     return instance;
   }

   getImage(key) {
     return this.imageMap.get(key);
   }

  onLoad(callback) {
    let loadCount = 0;
    for (let img of this.imageMap.values()) {
      img.onload = () => {
        loadCount++;
        if (loadCount == this.imageMap.size) {
          callback();
        }
      }
    }
  }

}